/*** return info ***/
function errorInfo(info){	
	return '<span class="judgeIcon redIcon"></span><span class="redTxt"><span>'+info+'</span></span>';
}
function tipsInfo(info){
	return '<span class="tip"><em class="row"></em><span class="tipCon">'+info+'</span></span>';
}
function successInfo(){
	return '<span class="judgeIcon blueIcon"></span>';
}
//for class inputA
function errorInfoA(info){
	return '<span class="judgeIcon redIcon"></span><span class="redTxtA"><span>'+info+'</span></span>';
}
function tipsInfoA(info){
	return '<span class="judgeIcon"></span><span class="yelTxtA"><span>'+info+'</span></span>';
}
function errorInfoTop(info){
	return '<span style="color:red">'+info+'</span>';
}

/*** define ***/
var arr = [0, 0, 0, 0];
var unTips = '6-16个字符（字母、数字、下划线）或3-8个中文';
var emailTips = '请填写您的真实邮箱地址';
var unNumTips = '用户名不允许为纯数字';
var pwTips = '6-16个字符，字母区分大小写';
var pwcTips = '请重复输入一次相同的登录密码';

/*** focus ***/
function showAccountNameTips(){
	showTips('accountName',unTips);
}
function showEmailTips(){
	showTips('email',emailTips);
}
function showPassportTips(){
	showTips('password',pwTips);
}
function showPasswordConfirmTips(){
	showTips('passwordConfirm',pwcTips);
}

/*** show ***/
function showTips(name,tips){
	$('#'+name+'Tips').empty();
	$('#'+name+'Tips').append(tipsInfo(tips));
    $('#'+name+'Tips').show();
	//$('#recommend').remove();
}

function showError(name,info){//show for class input , when error 
	$('#'+name+'Tips').empty();
	$('#'+name+'Tips').append(errorInfo(info));
    $('#'+name+'Tips').show();
}

function showErrorTop(name,info){//show for 
	var obj = $('#'+name);
	obj.empty();
	obj.append(errorInfoTop(info));
}

function showRight(name){
    $('#'+name+'Tips').hide();
}


/*** check ***/
function getLength(str){
	var i;
	var len;
	len = 0;
	for (i=0;i<str.length;i++)
	{
		if (str.charCodeAt(i)>255) len+=2; 
		else len++;
	}
	return len; 
}

function checkAccountName(){
	var accountName = $('[name=accountName]').val();
	$('[name=accountName]:last').removeClass('yelInput');
	$('[name=accountName]:last').addClass('grayInput');
	var length = 0;
	if(accountName=='')
	{
		showError('accountName','用户名是必填项');
		return false;
	}else {
		length = getLength(accountName);
		if(length <6 || length >16){
			showError('accountName',unTips);
			return false;
		}
	}
	$.ajax({
		type: "POST",
		dataType:"json",
		url: "/cms/account/existUser",
		data: {username:accountName},
		success: function(data){
			if(data.flag == true)
			{
				showRight('accountName');
				arr[0] = 1;
			} else {//用户已存在,生成推荐				
				$('#accountNameTips').empty();
				$('#accountNameTips').append(errorInfo('对不起，该用户名已被占用'));
			}
		}
	
	});
}

function checkEmail() {
	var email = $('[name=email]').val();
	if(email == '') {
		showError('email','邮箱是必填项');
		return false;
	} else {
		 var myRegExp = /[a-z0-9-]{1,30}@[a-z0-9-]{1,65}.[a-z]{3}/;
		 email = email.toLowerCase();
		 if(myRegExp.test(email)) {
			 showRight('email');
			 arr[1] = 1;
		 } else {
			 showError('email','邮箱格式不正确！');
		 }
	}
}

function checkPassword(){
    var i = 0; 
    var j=0;
	var password=$('[name=password]').val();
    for (i = 0; i< password.length; i++) {     
		if (password.charCodeAt(i) > 127) {
			showError('password',pwTips);
			return;
        } 
		j++;
    }
    if(j==0)
    {
    	showError('password',"密码是必填项");
        return;
    }
    if(j>16)
    {
       	showError('password','密码过长，'+pwTips);
		return;
    }	
    if(j<6)
    {
       	showError('password','密码过短，'+pwTips);
		return;
    }	
    if($('[name=passwordConfirm]').val()!='')
    {
    	checkConfPassword();
    }
    showRight('password');
    arr[2] = 1;
}

function checkConfPassword(){
	var password = $('[name=password]').val();
	var confPassword = $('[name=passwordConfirm]').val();
	if(confPassword==''){
		showError('passwordConfirm','确认密码是必填项');
	}
	else if($('[name=passwordConfirm]').val()==$('[name=password]').val()){
		showRight('passwordConfirm');
		arr[3] = 1;
	}
	else{
		showError('passwordConfirm','两次输入的密码不一致，请重新输入');
	}
}
 

function checkAll(){
	if(arr[1] == 1 && arr[2] == 1 && arr[3] == 1){
		return true;
	} else {
		return false;
	}
}